Skip to content

extra multicast/unicast predicates#114

Merged
taketo1113 merged 1 commit into
ruby:masterfrom
HoneyryderChuck:multicast-predicates
Jul 15, 2026
Merged

extra multicast/unicast predicates#114
taketo1113 merged 1 commit into
ruby:masterfrom
HoneyryderChuck:multicast-predicates

Conversation

@HoneyryderChuck

Copy link
Copy Markdown
Contributor

This adds the following predicate methods to IPAddr objects:

  • #multicast?: whether the ip address is multicast
  • #link_local_multicast?: whether the ip address is a link-local multicast
  • #global_unicast?: whetther the ip address is a global unicast

it also alias IPAddr#link_local? as IPAddr#link_local_unicast?, which is more accurate (whether the original should be deprecated is probably a separate discussion).

The motivation for adding these methods was to upstream these predicates from cel which have a broader meaning in the context of IP addressing, and maybe be applied in other contexts.

@HoneyryderChuck HoneyryderChuck requested a review from knu as a code owner June 22, 2026 12:17
@taketo1113

Copy link
Copy Markdown
Collaborator

@HoneyryderChuck What use cases do you have in mind for link_local_multicast? and global_unicast? ?

In particular, I'm not sure about the behavior of global_unicast? for IPv4.

According to RFC 4291, all IPv6 addresses except Unspecified (::/128), Loopback, Multicast, and Link-Local Unicast are defined as Global Unicast addresses:
https://datatracker.ietf.org/doc/html/rfc4291#section-2.4

However, there is no corresponding definition of "Global Unicast" for IPv4, so IPAddr would need to define what "Global Unicast" means for IPv4.

In the current implementation of this pull request, IPv4 addresses that are not globally reachable, such as Private addresses (e.g. 10.0.0.1) and Shared Address Space (e.g. 100.64.0.1), still return true.

IPAddr.new("10.0.0.1").global_unicast?
# => true

Is this behavior intentional? If so, could you explain the rationale behind it?

@HoneyryderChuck

Copy link
Copy Markdown
Contributor Author

@taketo1113 thx for the reply!

What use cases do you have in mind for link_local_multicast? and global_unicast? ?

I linked to the ruby implementation of the network package for the CEL language, and this particular patch is to allow a more standard and efficient way of implementing the IP package by moving the more general-purpose methods to the stdlib (just like in go's case, where these methods are part of the net.ip package.

However, there is no corresponding definition of "Global Unicast" for IPv4, so IPAddr would need to define what "Global Unicast" means for IPv4.

Indeed the RFCs aren't very clear about this. FWIW I took my definition from go's net.ip impl (relevant parts: "...with the exception of IPv4 directed broadcast addresses. It returns true even if ip is in IPv4 private address space or local IPv6 unicast address space", the last part also addresses one other question in the paragraph below).

@taketo1113 taketo1113 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HoneyryderChuck Thanks for the additional context.

I also looked at Go's implementation for reference.
However, there has been discussion about the behavior of net.IP.IsGlobalUnicast, particularly regarding IPv4 private addresses.

I also find it unintuitive for global_unicast? to return true for IPv4 private addresses.
If we introduce global_unicast? with this behavior, changing it later would create compatibility issues. For that reason, I don't think we should add global_unicast? at this point.

On the other hand, I think multicast? and link_local_multicast? are reasonable additions.
Could you update this PR to include only those two methods?

Comment thread lib/ipaddr.rb Outdated
Comment thread lib/ipaddr.rb
Comment thread lib/ipaddr.rb Outdated
Comment thread lib/ipaddr.rb Outdated
Comment thread lib/ipaddr.rb Outdated
Comment thread lib/ipaddr.rb Outdated
Comment thread test/test_ipaddr.rb Outdated
Comment thread lib/ipaddr.rb Outdated
This adds the following predicate methods to IPAddr objects:

* `#multicast?`: whether the ip address is multicast
* `#link_local_multicast?`: whether the ip address is a link-local
  multicast

it also alias `IPAddr#link_local?` as `IPAddr#link_local_unicast?`,
which is more accurate (whether the original should be deprecated is
probably a separate discussion).
@HoneyryderChuck

Copy link
Copy Markdown
Contributor Author

@taketo1113 done. I still think that a #global_unicast? method makes sense, but I agree that we should perhaps discuss what to do with IPv4 addresses first before merging it. I moved it to a separate PR, lmk where that discussion should proceed, or at all.

@taketo1113 taketo1113 merged commit 2f25173 into ruby:master Jul 15, 2026
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants